﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphCompiler.Core.SyntaxRepresentation;
using GraphCompiler.Core.Exceptions;

namespace GraphCompiler.Core.Parsing
{
    public class DiagramParser : ParserBase<DiagramNode>
    {
        public DiagramParser(ITypedParser<BoxNode> boxParser, ITypedParser<ArrowNode> arrowParser)
        {
            AddChildParser(boxParser);
            AddChildParser(arrowParser);
        }

        public override DiagramNode ParseTypedNode(TokenCollection tokens)
        {
            _tokens = tokens;

            ConsumeToken("diagram");
            ConsumeToken("{");

            var diagramNode = CreateDiagramNode();

            ParseAndAddChildElements();

            ConsumeToken("}");

            return diagramNode;
        }

        private DiagramNode CreateDiagramNode()
        {
            var diagramNode = new DiagramNode();
            Node = diagramNode;

            return diagramNode;
        }

        public override bool IsApplicable(TokenCollection tokens)
        {
            return _tokens.First().Equals(new Token("diagram"));
        }
    }
}
